package com.wn.class30.service.impl;

import com.wn.class30.entity.UserExample;
import com.wn.class30.mapper.BusinessDao;
import com.wn.class30.mapper.MyorderDao;
import com.wn.class30.mapper.ProductDao;
import com.wn.class30.mapper.UserDao;
import com.wn.class30.service.BackUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

@Slf4j
@Service
public class BackUserServiceImpl implements BackUserService {
    @Resource
    private UserDao userDao;
    @Resource
    private ProductDao productDao;
    @Resource
    private BusinessDao businessDao;
    @Resource
    private MyorderDao myorderDao;
    @Override
    public int selectCount(Map map) throws Exception{
        UserExample ex = new UserExample();
        String username = (String) map.get("username");
        String tel = (String) map.get("tel");
        String lvId = (String) map.get("lvId");

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = (String) map.get("startDate");
        String endDate = (String) map.get("endDate");
        Integer lvid = 0;

        UserExample.Criteria criteria = ex.createCriteria();
        if(!"".equals(username) && username != null){
            criteria.andUsernameLike("%"+username+"%");
        }
        if(lvId!=null&&!"".equals(lvId)){
            lvid = Integer.parseInt(lvId);
            criteria.andLvIdEqualTo(lvid);
        }
        if(!"".equals(tel) && tel != null){
            criteria.andTelEqualTo(tel);
        }
        if(!"".equals(startDate) && startDate != null){
            criteria.andRegistertimeGreaterThanOrEqualTo(simpleDateFormat.parse(startDate));
        }

        if(!"".equals(endDate) && endDate != null){
            Date date = simpleDateFormat.parse(endDate);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, 1);
            Date time = c.getTime();

            criteria.andRegistertimeLessThanOrEqualTo(time);
        }
        return (int) userDao.countByExample(ex);
    }

    @Override
    public Object select(Map map) throws Exception{
        UserExample ex = new UserExample();
        String username = (String) map.get("username");
        String tel = (String) map.get("tel");
        String lvId = (String) map.get("lvId");
        Integer page = 0;
        if(!"".equals((String) map.get("page"))&&(String) map.get("page")!=null){
            page = Integer.parseInt((String) map.get("page"));
        }
        Integer limit = 0;
        if(!"".equals((String) map.get("limit"))&&(String) map.get("limit")!=null){
            limit = Integer.parseInt((String) map.get("limit"));
        }
        ex.setOffset(((long)(page-1))*limit);
        ex.setLimit(limit);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = (String) map.get("startDate");
        String endDate = (String) map.get("endDate");
        Integer lvid = 0;
        UserExample.Criteria criteria = ex.createCriteria();
        if(!"".equals(username) && username != null){
            criteria.andUsernameLike("%"+username+"%");
        }
        if(lvId!=null&&!"".equals(lvId)){
            lvid = Integer.parseInt(lvId);
            criteria.andLvIdEqualTo(lvid);
        }
        if(!"".equals(tel) && tel != null){
            criteria.andTelEqualTo(tel);
        }
        if(!"".equals(startDate) && startDate != null){
            criteria.andRegistertimeGreaterThanOrEqualTo(simpleDateFormat.parse(startDate));
        }

        if(!"".equals(endDate) && endDate != null){
            Date date = simpleDateFormat.parse(endDate);
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, 1);
            Date time = c.getTime();

            criteria.andRegistertimeLessThanOrEqualTo(time);
        }
        return userDao.selectByExample(ex);
    }

    @Override
    public Object selectUserCount() {
        return userDao.countByExample(null);
    }

    @Override
    public Object selectProCount() {
        return productDao.countByExample(null);
    }

    @Override
    public Object selectBusiCount() {
        return businessDao.countByExample(null);
    }

    @Override
    public Object selectOrderCount() {
        return myorderDao.countByExample(null);
    }
}
